//
//  RegisterViewController.m
//  DoubanDemo1
//
//  Created by lanou3g on 15/11/21.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterView.h"
#import "LTView.h"
#import "DataUser.h"

#import "DataBase.h"
#import "UserInformation.h"
#import "FileHandle.h"
@interface RegisterViewController ()

@property (nonatomic,strong)RegisterView *registerView;

@end

@implementation RegisterViewController

- (void)loadView
{
    self.registerView = [[RegisterView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.registerView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //左侧用户登录按钮
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithTitle:@"<用户登录" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemAction:)];
    self.navigationItem.leftBarButtonItem = leftItem;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"呵呵" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemAction:)];
    // Do any additional setup after loading the view.
}

//左侧用户登录按钮方法
- (void)leftItemAction:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//- (void)rightItemAction:(UIBarButtonItem *)sender
//{
//    NSLog(@"11");
//    
//}

- (void)rightItemAction:(UIBarButtonItem *)sender
{
    NSLog(@"%@",self.registerView.userNameLTView.textField.text);
    NSString *userStr =  self.registerView.userNameLTView.textField.text;
    NSString *passwordStr = self.registerView.passWordLTView.textField.text;
    NSString *repasswordStr = self.registerView.confirmLTView.textField.text;
//    NSLog(@"%@",userStr);
    if (userStr.length == 0 || passwordStr.length == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"密码不能为空" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [alertView show];
        NSTimer *time = [NSTimer timerWithTimeInterval:1 target:alertView selector:@selector(dismissWithClickedButtonIndex:animated:) userInfo:nil repeats:NO];
        [alertView dismissWithClickedButtonIndex:1 animated:NO];
    }else if([passwordStr isEqualToString:repasswordStr] == NO){
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"两次密码必须一致" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [alertView show];
        NSTimer *time = [NSTimer timerWithTimeInterval:1 target:alertView selector:@selector(dismissWithClickedButtonIndex:animated:) userInfo:nil repeats:NO];
        [alertView dismissWithClickedButtonIndex:1 animated:NO];
    }else{
        if ([[DataBase sharedDataBase] selectInformationWithUserName:userStr].uesrName != nil) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"账号已经注册" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [alertView show];
            NSTimer *time = [NSTimer timerWithTimeInterval:1 target:alertView selector:@selector(dismissWithClickedButtonIndex:animated:) userInfo:nil repeats:NO];
            [alertView dismissWithClickedButtonIndex:1 animated:NO];
        }else{
            NSArray *movieArray = [NSArray array];
            NSArray *activityArray = [NSArray array];
            UserInformation *userInfortion = [[UserInformation alloc] initWithUserName:userStr password:passwordStr telephone:self.registerView.phoneNumberLTview.textField.text email:self.registerView.emailLTView.textField.text movieAarry:movieArray activity:activityArray];
            NSLog(@"------%@",userInfortion);
            [[DataBase sharedDataBase] insertUserInformation:userInfortion];
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"注册成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [alertView show];
            NSTimer *time = [NSTimer timerWithTimeInterval:1 target:alertView selector:@selector(dismissWithClickedButtonIndex:animated:) userInfo:nil repeats:NO];
            
            [[FileHandle shareInstance] setUsername:userStr];
          
            [[FileHandle shareInstance] user];
            [[FileHandle shareInstance] synchronize];
            [alertView dismissWithClickedButtonIndex:1 animated:NO];
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    }
 
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
